﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Panda.DynamicWebApi;
using Panda.DynamicWebApi.Attributes;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WareHose.Application.Admins.Dto;
using WareHose.Application.Roles.DTO;
using WareHose.Application.TaskStocks.DTO;
using WareHouse.Domain;
using WareHouse.Repositories;

namespace WareHose.Application.TaskStocks
{
    [DynamicWebApi]
    [ApiExplorerSettings(GroupName = "v2")]
    public class TaskStockRepostion:IDynamicWebApi
    {
        //仓储
        private readonly IBaseRepository<Taskstock> db;
        //AutoMapper
        private readonly IMapper mapper;
        private readonly ISqlSugarClient sqlSugarClient;


        public TaskStockRepostion(IBaseRepository<Taskstock> db, IMapper mapper, ISqlSugarClient sqlSugarClient)
        {
            this.db = db;
            this.mapper = mapper;
            this.sqlSugarClient = sqlSugarClient;
        }




        /// <summary>
        /// 盘点添加
        /// </summary>
        /// <param name="RoleInputDTo"></param>
        /// <returns></returns>

        [HttpPost]
        [AllowAnonymous]
        public async Task<ResultDto> AddUpdAsync(TaskStocksDTo TaskStocksDTo)
        {
            try
            {

                var entity = mapper.Map<TaskStocksDTo, Taskstock>(TaskStocksDTo);

                //entity.AddPerson = "刘晓雯";

                //entity.AddDate = DateTime.Now;
                await db.InsertAsync(entity);
                return new ResultDto { Code = Status.Success, Message = "添加成功" };

            }
            catch (Exception e)
            {
                return new ResultDto { Code = Status.Failed, Message = e.Message };
            }
        }



        /// <summary>
        /// 盘点显示
        /// </summary>
        /// <param name="RoleRequestDTo"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultDtos<List<TaskStocksDTo>>> AdminPage(string? CheckCode, DateTime? AddDate, int State, int pageIndex = 1, int pageSize = 4)
        {





            var list = (from a in await db.QueryAsync()
                        select new TaskStocksDTo
                        {
                            AddDate = a.AddDate,
                            State = a.State,
                            CheckId = a.CheckId,
                            CheckCode = a.CheckCode,
                            CheckType = a.CheckType,
                            Remarks = a.Remarks,
                            AddPerson = a.AddPerson,
                        });
            if (!string.IsNullOrEmpty(CheckCode))
            {
                list = list.Where(m => m.CheckCode.Contains(CheckCode));
            }
            if (AddDate!=null)
            {
                list = list.Where(m => m.AddDate>= AddDate);
            }   
            if (State != 0)
            {
                list = list.Where(m => m.State == State);
            }
            int listcount = list.Count();
            list = list.OrderBy(x => x.CheckId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();


            //RefAsync<int> totalCount = 0;
            //RefAsync<int> totalPage = 0;

            //var list = await sqlSugarClient.Queryable<Taskstock>()
            //         .Select((a) => new TaskStocksDTo
            //         { 
            //              AddDate =a. AddDate,
            //              State =a. State,
            //               CheckId=a.CheckId,
            //               CheckCode = a.CheckCode,
            //               CheckType=a.CheckType,
            //               Remarks=a.Remarks,
            //               AddPerson=a.AddPerson,


            //           })
            //.WhereIF(!string.IsNullOrEmpty(CheckCode),a=>a.CheckCode.Contains(CheckCode))
            //.WhereIF(AddDate!=null,a=>a.AddDate >= AddDate)
            //.WhereIF(State!=null|| State!=0,a=>a.State== State)
            //.ToPageListAsync(pageIndex, pageSize, totalCount, totalPage);

            return new ResultDtos<List<TaskStocksDTo>>
            {
                Code = Status.Success,
                Data = list.ToList(),
               Listcount=listcount,
            };
        }



        /// <summary>
        /// 盘点删除  就这换成批量的
        /// </summary>
        /// <param name="RoldId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResultDto> DeleRoleAsync(string CheckId)
        {
            if(CheckId!=null)
            {
                string[] ids = CheckId.Split(',');
                foreach (var item in ids)
                {
                    await db.DeleAsync(Convert.ToInt64(item));
                }
                return new ResultDto { Code = Status.Success, Message = "删除成功" };
            }
            else
            {
                return new ResultDto { Code = Status.Success, Message = "删除失败" };
            }

        }







        /// <summary>
        /// 盘点修改
        /// </summary>
        /// <param name="RoleInputDTo"></param>
        /// <returns></returns>

        [HttpPut]
        public async Task<ResultDto> RoleUpdateAsync(TaskStocksDTo TaskStocksDTo)
        {
            try
            {

                var entity = mapper.Map<TaskStocksDTo, Taskstock>(TaskStocksDTo);
                await db.UpdateAsync(entity);
                return new ResultDto { Code = Status.Success, Message = "修改成功" };

            }
            catch (Exception e)
            {
                return new ResultDto { Code = Status.Failed, Message = e.Message };
            }
        }


        /// <summary>
        /// 盘点反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet]
        public async Task<ResultDto<Taskstock>> BackFill(long CheckId)
        {
            var list = await db.FistAsync(x => x.CheckId == CheckId);

            return new ResultDto<Taskstock> { Code = Status.Success, Message = "成功", Data = list };
        }


    }
}
